#!/usr/bin/python3
# Used to check cameras before committing to install
# Executed before primary apt install of files

import subprocess
import sys
import os

# Backup the config file if we're upgrading
if "upgrade" in sys.argv:
	# Try to copy the config file as a backup
	try:
		# Try to copy the new location first
		if os.path.exists("/etc/howdy/config.ini"):
			subprocess.call(["cp /etc/howdy/config.ini /tmp/howdy_config_backup_v" + sys.argv[2] + ".ini"], shell=True)
		# If that does not exist, try copying the old location
		else:
			subprocess.call(["cp /lib/security/howdy/config.ini /tmp/howdy_config_backup_v" + sys.argv[2] + ".ini"], shell=True)

		# Let the user know so he knows where to look on a failed install
		print("Backup of Howdy config file created in /tmp/howdy_config_backup_v" + sys.argv[2] + ".ini")
	except subprocess.CalledProcessError:
		print("Could not make an backup of old Howdy config file")

	# Don't continue setup when we're just upgrading
	sys.exit(0)

# Don't run if we're not trying to install fresh
if "install" not in sys.argv:
	sys.exit(0)
